package com.hobee.basic.controller;

import com.hobee.basic.config.scheduling.DynamicSchedule;
import com.hobee.basic.model.BdModul;
import com.hobee.basic.model.es.ClassType;
import com.hobee.basic.service.ClassTypeService;
import com.hobee.basic.service.ModulService;
import com.hobee.basic.utils.DateUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;

/**
 * @author junjie.li
 * @date 2020/7/20 15:25
 */
@RestController
@RequestMapping("test")
public class TestController {

    private final RabbitTemplate template;

    private final ClassTypeService classTypeService;

    private final ModulService modulService;

    private final DynamicSchedule dynamicSchedule;

    @Autowired
    public TestController(RabbitTemplate template, ClassTypeService classTypeService, ModulService modulService, DynamicSchedule dynamicSchedule) {
        this.template = template;
        this.classTypeService = classTypeService;
        this.modulService = modulService;
        this.dynamicSchedule = dynamicSchedule;
    }

    @RequestMapping("/hello")
    public Object hello() {
        return "hello world";
    }

    @RequestMapping("/mq/send")
    public void send() {
        template.convertAndSend("niceloo", "nicelooBoot.test", "hello rabbit");
    }


    @RequestMapping("/es/initdata")
    public void initData() {
        classTypeService.initData();
    }

    @GetMapping("/es/query")
    public List<ClassType> queryClassTypeByEs(@RequestParam String classtypeName, @RequestParam String projectId) {
        return classTypeService.queryClassTypeByEs(classtypeName, projectId);
    }

    @RequestMapping(value = "/model/id")
    public BdModul info(@RequestParam(value = "modulId") String modulId) {
        return modulService.findById(modulId);
    }

    @RequestMapping(value = "/model/update")
    public void update(@RequestParam(value = "modulId") String modulId) {
        modulService.updateBySelective(this.info(modulId));
    }

    @RequestMapping(value = "/schedule")
    public void mockSchedule() {
        List<String> times = Arrays.asList("2020-09-20 14:35:00", "2020-09-20 14:37:00");
        times.forEach(time -> {
            LocalDateTime dateTime = LocalDateTime.parse(time, DateTimeFormatter.ofPattern(DateUtils.TIME_YMDHMS_PATTERN));
            String cron = dateTime.getSecond() + " " + dateTime.getMinute() + " " + dateTime.getHour()
                    + " " + dateTime.getDayOfMonth() + " " + dateTime.getMonth().getValue() + " ?";

            dynamicSchedule.dynamicAdd(new Ai(), cron);
        });


    }

    /**
     * 模拟随机更新操作
     */
    class Ai implements Runnable {

        @Override
        public void run() {
            System.out.println("模拟进行更新操作");
            BdModul modul = modulService.findById("MODUL20191126010000000007");
            Assert.notNull(modul, "模块不存在");
        }
    }


}
